package com.momo.todo.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.momo.todo.R;
import com.momo.todo.model.PlanModel;
import com.momo.todo.utils.LogUtils;

import org.w3c.dom.Text;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.SimpleFormatter;

public class AddPlanActivity extends AppCompatActivity {

    private EditText et_plan_content;

    private List<TextView> tagList;
    private TextView tv_label_study;
    private TextView tv_label_life;
    private TextView tv_label_other;

    private ImageButton ib_back;
    private TextView tv_ok;

    private int chosenTag;
    private int requestType;

    private boolean isTop;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_plan);

        findViews();

        initData();

        setListener();
    }

    private void initData() {
        tagList = new ArrayList<>();
        tagList.add(tv_label_life);
        tagList.add(tv_label_other);
        tagList.add(tv_label_study);

        chosenTag = -1;
        choseTag(chosenTag);
        isTop = false;

        requestType = getIntent().getIntExtra("request",MainActivity.REQUEST_CODE_ADD_PLAN);
        if(requestType == MainActivity.REQUEST_CPDE_EDIT_PLAN){
            PlanModel plan = (PlanModel) getIntent().getSerializableExtra("planModel");
            if(plan != null){
                choseTag(plan.getType());
                et_plan_content.setText(plan.getContent());
                isTop = plan.isTop();
            }
        }

    }

    private void choseTag(int tag){
        for(int i=0;i<tagList.size();i++){
            tagList.get(i).setBackgroundResource(R.drawable.bg_label);
        }

        if(tag == PlanModel.PLAN_TYPE_LIFE){
            tv_label_life.setBackgroundResource(R.drawable.bg_label_life);
            et_plan_content.setTextColor(getResources().getColor(R.color.darkBlue));
            chosenTag = PlanModel.PLAN_TYPE_LIFE;
        }
        if(tag == PlanModel.PLAN_TYPE_STUDY){
            tv_label_study.setBackgroundResource(R.drawable.bg_label_study);
            et_plan_content.setTextColor(getResources().getColor(R.color.darkPink));
            chosenTag = PlanModel.PLAN_TYPE_STUDY;
        }
        if(tag == PlanModel.PLAN_TYPE_OTHER){
            tv_label_other.setBackgroundResource(R.drawable.bg_label_other);
            et_plan_content.setTextColor(getResources().getColor(R.color.darkGreen));
            chosenTag = PlanModel.PLAN_TYPE_OTHER;
        }
    }

    private void setListener() {

        for(int i=0;i<tagList.size();i++){
            tagList.get(i).setOnClickListener(new TextViewListener());
        }

        ib_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        tv_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(chosenTag == -1){
                    LogUtils.toast(AddPlanActivity.this,"请选择一个标签");
                }else if(et_plan_content.getText().toString().trim().equals("")){
                    LogUtils.toast(AddPlanActivity.this,"计划内容不能为空");
                }else {
                    Intent intent = new Intent();
                    Bundle bundle = new Bundle();

                    PlanModel planModel = new PlanModel();

                    planModel.setContent(et_plan_content.getText().toString());
                    planModel.setType(chosenTag);
                    planModel.setTime(System.currentTimeMillis());
                    planModel.setTop(isTop);

                    bundle.putSerializable("planModel", planModel);
                    intent.putExtras(bundle);
                    setResult(RESULT_OK, intent);

                    et_plan_content.setText("");

                    if(requestType == MainActivity.REQUEST_CPDE_EDIT_PLAN){
                        LogUtils.toast(AddPlanActivity.this,"编辑成功");
                    }else{
                        LogUtils.toast(AddPlanActivity.this,"添加成功");
                    }
//                LogUtils.toast(AddPlanActivity.this,"添加成功");
                    finish();
                }
            }
        });


    }

    private class TextViewListener implements View.OnClickListener{

        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.tv_label_study:
                    choseTag(PlanModel.PLAN_TYPE_STUDY);
                    break;
                case R.id.tv_label_life:
                    choseTag(PlanModel.PLAN_TYPE_LIFE);
                    break;
                case R.id.tv_label_other:
                    choseTag(PlanModel.PLAN_TYPE_OTHER);
                    break;
            }
        }
    }
    private void findViews() {
        et_plan_content = (EditText) findViewById(R.id.et_plan_content);

        tv_label_study = (TextView) findViewById(R.id.tv_label_study);
        tv_label_life = (TextView) findViewById(R.id.tv_label_life);
        tv_label_other = (TextView) findViewById(R.id.tv_label_other);

        ib_back = (ImageButton) findViewById(R.id.ib_back);
        tv_ok = (TextView) findViewById(R.id.tv_ok);
    }
}
